﻿/*
Copyright Tophy 4/16/2006
滑动条行为类
*/

dynamic class Scroll{
	//---------------------------------------  变量
	/////////////  私有变量
	
	private var min:Number;
	private var max:Number;
	private var value:Number;
	private var mbWZ:Number;
	private var _mbvalue:Number;
	private var moveId:Number;
	private var interN:Number;
	private var stepN:Number;
	private var parent:MovieClip;
	private var _system:MovieClip;
	
	private var me:Scroll;
	
	private var enableScroll:Boolean
	
	//////////////   公共变量
	public var VorH:Boolean;
	public var Mirror:Boolean;
	public var name:String;
	public var StartDrag:Boolean;
	public var hand:MovieClip;
	public var bar:MovieClip;
	//////////////    事件
	public var scroll:Function;  		//原型 scroll(n)
	public static var say:Function;		//原型 say(o)
	////////////////////////////////////////////////////////////     
	public function Scroll(vorh:Boolean,Hand:MovieClip,Bar:MovieClip){
		VorH=vorh;
		hand=Hand;
		bar=Bar;
		initVars();
		initSystem();
		Say("初始化完成!");
	}
	private function initSystem(){
		bar.me=me;
		hand.me=me;
		hand._x=bar._x;
		hand._y=bar._y;
		if(VorH){
			mbWZ=bar._x;
		}else{
			mbWZ=bar._y;
		}
		hand.onPress=OnBarPress;
		hand.onRelease=hand.onReleaseOutside=OnBarRelease;
		var obj:Object=new Object();
		obj.me=me;
		obj.onMouseMove=OnMouseMove;
		Mouse.addListener(obj);
		////////////////////////           设置动态参数
		interN=16;
		stepN=10;
		/////////////////////
		this.enableScroll=true;
	}
	public function MoveFun(me:Object){
		me.StartDrag=true;
		var run:Boolean=true;
		var l:Number=0;
		if(me.VorH){
			l=(me.mbWZ-me.hand._x);
			if(Math.abs(l)<=1){
				run=false;
				me.hand._x=me.mbWZ;
			}else{
				me.hand._x+=l/me.stepN;
			}
		}else{
			l=(me.mbWZ-me.hand._y);
			if(Math.abs(l)<=1){
				run=false;
				me.hand._y=me.mbWZ;
			}else{
				me.hand._y+=l/me.stepN;
			}
		}
		me.BroadcaseScrollMsg(me.MouseMoveFun());
		if(!run){
			clearInterval(me.moveId);
			me.moveId=-1
			me.StartDrag=false;
		}
	}
	private function OnMouseMove(){
		me._mbvalue=me.MouseMoveFun();
		me.BroadcaseScrollMsg(me._mbvalue);
	}
	private function MouseMoveFun():Number{
		var sx=this.me.bar._x;
		var sy=this.me.bar._y;
		var lx=this.me.bar._width;
		var ly=this.me.bar._height;
		var vx=this.me.hand._x;
		var vy=this.me.hand._y;
		var v:Number=0;
		if(this.me.VorH){
			v=Math.abs((vx-sx)/lx)*(this.me.Max-this.me.Min);
		}else{
			v=Math.abs((vy-sy)/ly)*(this.me.Max-this.me.Min);
		}
		v+=this.me.Min;
		
		return v;
	}
	private function BroadcaseScrollMsg(v:Number){
		if(this.me.StartDrag){
			this.me.value=v;
			if(this.me.scroll!=undefined){
				this.me.scroll(v);
			}
		}
	}
	private function OnBarRelease(){
		this.me.StartDrag=false;
		this.stopDrag();
	}
	private function OnBarPress() {
		//trace(this.me.enableScroll + "|" + this.me.VorH + "|" + this.me.Mirror + "|"+this.me.hand);
		if(!this.me.enableScroll) return;
		this.me.StartDrag=true;
		if(this.me.VorH){
			if(this.me.Mirror){
				this.startDrag(false,this.me.bar._x,this.me.bar._y,this.me.bar._x-this.me.bar._width,this.me.bar._y);
			}else{
				this.startDrag(false,this.me.bar._x,this.me.bar._y,this.me.bar._x+this.me.bar._width,this.me.bar._y);
			}
		}else{
			if(this.me.Mirror){
				this.startDrag(false,this.me.bar._x,this.me.bar._y,this.me.bar._x,this.me.bar._y-this.me.bar._height);
			}else{
				this.startDrag(false,this.me.bar._x,this.me.bar._y,this.me.bar._x,this.me.bar._y+this.me.bar._height);
			}
		}
	}
	private function initVars(){
		me=this;
		parent=bar._parent;
		if(VorH==undefined){
			VorH=true;
		}
		min=0;
		max=100;
		value=0;
		name="noName";
		StartDrag=false;
		moveId=-1;
		Mirror=false;
	}
	
	/////////////////////////   属性
	public function set Min(n:Number):Void{
		if(me.max<n){
			Say("**警告**最小值不能大于最大值");
		}else{
			me.min=n;
			if(me.value<me.min)
			{
				me.Value=n;
			}else{
				me.Value=me.value;
			}
		}
	}
	public function get Min():Number{
		return me.min;
	}
	
	public function set Max(n:Number):Void{
		if(me.min>n){
			Say("**警告**最大值不能小于最小值");
		}else{
			me.max=n;
			if(me.value>me.max){
				me.Value=n;
			}else{
				me.Value=me.value;
			}
		}
	}
	public function get Max():Number{
		return me.max;
	}
	public function set Value(n:Number):Void{
		if(n<me.min || n>me.max){
			Say("**警告**值设置不能小于最小值，或大于最大值");
		}else{
			var l=me.max-me.min;
			var wz=n-me.min;
			if(this.me.Mirror){
				wz=-wz;
			}
			if(VorH){
				var lx=bar._width;
				me.mbWZ=((wz/l)*lx)+bar._x;
			}else{
				var ly=bar._height;
				me.mbWZ=((wz/l)*ly)+bar._y;
			}
			if(me.moveId<0){
				me.moveId=setInterval(me.MoveFun,me.interN,me);
			}
		}
	}
	public function get Value():Number{
		return me.value;
	}
	
	public function get MBValue():Number{
		return me._mbvalue;
	}
	
	public function get Enable():Boolean{
		return me.enableScroll;
	}
	public function set Enable(value:Boolean){
		me.enableScroll=value;
	}
	/////////////////////////////////////////////////
	private function Say(o){
		say("SCROLL["+name+"]："+o);
	}
}